package com.gzyj.jtt808.v2011.protocol;

import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.enums.BusinessDataType;
import com.gzyj.jtt808.v2011.enums.JT808TerminalResult;
import com.gzyj.jtt808.v2011.Package;
import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.io.Serializable;

/**
 * 平台通用应答
 *
 * @author lhj
 */
@Data
public class JTT808_0x8001 extends Package implements Serializable {

    private short MsgNum;//应答消息流水号
    private short AckMsgId;//应答消息Id

    private JT808TerminalResult Result;//返回结果

    public JTT808_0x8001() {
        super(BusinessDataType.平台通用应答.getCode());
    }

    @Override
    public void encodeImpl(ByteBuf buf) {
        buf.writeShort(this.MsgNum);
        buf.writeShort(this.AckMsgId);
        buf.writeByte(this.Result.getCode());
    }

    @Override
    public JTT808_0x8001 decodeImpl(ByteBuf buf) {
        byte[] temp = HexBytesUtil.readByOffset(buf, 2);
        this.MsgNum = Short.parseShort(HexBytesUtil.bytesToHex(temp), 16);
        temp = HexBytesUtil.readByOffset(buf, 2);
        this.AckMsgId = Short.parseShort(HexBytesUtil.bytesToHex(temp), 16);
        temp = HexBytesUtil.readByOffset(buf, 1);
        int code = Integer.parseInt(HexBytesUtil.bytesToHex(temp), 16);
        this.Result = JT808TerminalResult.find(code);


        return this;
    }


}
